from . import api_blueprint
from app.model.course import Course
from flask import request, json
from app import db
from app.model.teacher import Teacher

# get courses by page num
@api_blueprint.route('/course/<int:page_num>', methods=['GET'])
def get_by_page(page_num=1):
    courses = Course.query.paginate(page_num, 10, False).items
    # print(courses)
    return json.dumps(courses, default=Course.to_json)

# search courses
@api_blueprint.route('/course/search', methods=['GET'])
def get_courses_by_keywords():
    keywords = request.values.get('keywords')
    print(keywords)
    courses = Course.query.filter(Course.Cos_name.like('%'+keywords+'%')).all()
    # print(courses)
    return json.dumps(courses, default=Course.to_json)

# get all courses
@api_blueprint.route('/course/all', methods=['GET'])
def get_all_courses():
    courses = Course.query.all()
    # print(courses)
    return json.dumps(courses, default=Course.to_json)


@api_blueprint.route('/course', methods=['GET'])
def get_course_by_id():
    cos_id = request.values.get('Cos_id')
    if cos_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'Cos_id is missing'
        }), 400
    course = Course.query.filter(Course.Cos_id == cos_id).first()
    if course is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target course is not found'
        }), 404
    # print(courses)
    return json.dumps(course, default=Course.to_json)

# delete course
@api_blueprint.route('/course', methods=['DELETE'])
def delete_course():
    cos_id = request.values.get('Cos_id')
    if cos_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'Cos_id is missing'
        }), 400

    # print(courses)
    row = Course.query.filter(Course.Cos_id == cos_id).first()
    if row is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target course is not found'
        }), 404
    try:
        db.session.delete(row)
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    # return cos_id
    return json.dumps({
        'status': 'success'
    })

# modify course
@api_blueprint.route('/course/<string:cos_id>', methods=['PUT'])
def modify_course(cos_id):
    course = Course.query.filter(Course.Cos_id == cos_id).first()

    if course is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target course is not found'
        }), 404

    if request.values.get('Cos_name') is not None:
        course.Cos_name = request.values.get('Cos_name')
    if request.values.get('Cos_credit') is not None:
        course.Cos_credit = request.values.get('Cos_credit')
    if request.values.get('Cos_teacher') is not None:
        course.Cos_teacher = request.values.get('Cos_teacher')
    if request.values.get('Cos_intro') is not None:
        course.Cos_intro = request.values.get('Cos_intro')
    if request.values.get('Cos_time') is not None:
        course.Cos_time = request.values.get('Cos_time')
    if request.values.get('Cos_site') is not None:
        course.Cos_site = request.values.get('Cos_site')
    if request.values.get('Cos_college') is not None:
        course.Cos_college = request.values.get('Cos_college')
    if request.values.get('Cos_stime') is not None:
        course.Cos_stime = request.values.get('Cos_stime')
    if request.values.get('Cos_etime') is not None:
        course.Cos_etime = request.values.get('Cos_etime')
    if request.values.get('Cos_maxNum') is not None:
        course.Cos_maxNum = request.values.get('Cos_maxNum')
    if request.values.get('Cos_num') is not None:
        course.Cos_num = request.values.get('Cos_num')

    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })

# 设置课程是否可选
@api_blueprint.route('/course/set/<string:cos_id>', methods=['PUT'])
def set_course_selectable(cos_id):
    if request.values.get('selectable') is None:
        return json.dumps({
            'status': 'error',
            'msg': 'selectable is missing'
        }), 400

    row = Course.query.filter(Course.Cos_id == cos_id).first()

    if row is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target course is not found'
        }), 404
    row.Cos_selectable = request.values.get('selectable')
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })

# add course
@api_blueprint.route('/course', methods=['POST'])
def add_course():
    cos_id = request.values.get('Cos_id')
    if cos_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'Cos_id is missing'
        }), 400

    course = Course()

    if request.values.get('Cos_id') is not None:
        course.Cos_id = request.values.get('Cos_id')
    if request.values.get('Cos_name') is not None:
        course.Cos_name = request.values.get('Cos_name')
    if request.values.get('Cos_credit') is not None:
        course.Cos_credit = request.values.get('Cos_credit')
    if request.values.get('Cos_teacher') is not None:
        course.Cos_teacher = request.values.get('Cos_teacher')
    if request.values.get('Cos_intro') is not None:
        course.Cos_intro = request.values.get('Cos_intro')
    if request.values.get('Cos_time') is not None:
        course.Cos_time = request.values.get('Cos_time')
    if request.values.get('Cos_site') is not None:
        course.Cos_site = request.values.get('Cos_site')
    if request.values.get('Cos_college') is not None:
        course.Cos_college = request.values.get('Cos_college')
    if request.values.get('Cos_stime') is not None:
        course.Cos_stime = request.values.get('Cos_stime')
    if request.values.get('Cos_etime') is not None:
        course.Cos_etime = request.values.get('Cos_etime')
    if request.values.get('Cos_maxNum') is not None:
        course.Cos_maxNum = request.values.get('Cos_maxNum')
    else:
        course.Cos_maxNum = 0
    if request.values.get('Cos_num') is not None:
        course.Cos_num = request.values.get('Cos_num')
    else:
        course.Cos_num = 0
    course.Cos_selectable = 0

    db.session.add(course)

    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })
